package com.shoulder.boot.system.exception;

import com.shoulder.core.api.ApiResult;
import com.shoulder.resourceserver.exception.ResourceExceptionHandler;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Slf4j
@RestControllerAdvice
public class SystemExceptionHandler extends ResourceExceptionHandler {
    @ExceptionHandler
    @Override
    public ApiResult<Object> handleException(Exception ex) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        if (ex instanceof SystemException) {
            log.error("系统中心发生异常:{},url = {}", ex.getMessage(), request.getRequestURI(), ex);
            return ApiResult.badRequest(null, ex.getMessage());
        }
        return super.handleException(ex);
    }
}
